package com.s59.rbac.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.s59.rbac.common.LoginUser;
import com.s59.rbac.common.Token;
import com.s59.rbac.entity.Menu;
import com.s59.rbac.entity.Role;
import com.s59.rbac.entity.User;
import com.s59.rbac.entity.vo.LoginVO;
import com.s59.rbac.mapper.UserMapper;
import com.s59.rbac.redis.RedisUtils;
import com.s59.rbac.service.ILoginService;
import com.s59.rbac.service.IMenuService;
import com.s59.rbac.service.IRoleService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.security.auth.login.AccountNotFoundException;
import javax.security.auth.login.LoginException;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import static com.s59.rbac.common.Constants.CACHE_PREFIX_TOKEN;

@Service
public class LoginServiceImpl extends ServiceImpl<UserMapper, User> implements ILoginService {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private PermssionService permssionService;

    @Override
    @SneakyThrows
    public Token login(LoginVO loginVO) throws LoginException {
        User user = getBaseMapper().selectOne(Wrappers.lambdaQuery(User.class).eq(User::getUserName, loginVO.getUserName()));
        if (user == null){
            throw new LoginException("用户不存在");
        }
        if (!user.getPassword().equals(loginVO.getPassword())){
            throw new LoginException("密码不正确");
        }
        // 获取用户角色
//        List<Role> roleList = roleService.getByUserName(user.getUserName());
//        if (roleList.isEmpty()){
//            throw new LoginException("请先授权");
//        }
        // 获取用户的权限
//        List<Long> roleIdList = roleList.stream().mapToLong(Role::getRoleId).boxed().collect(Collectors.toList());
//        List<Menu> menuList = menuService.getByRoleIdList(roleIdList);
//        // 缓存到redis
//        Set<String> roleKeyList = roleList.stream().map(Role::getRoleKey).collect(Collectors.toSet());
//        Set<String> permsList = menuList.stream()
//                .filter(item -> StringUtils.hasText(item.getPerms()))
//                .map(Menu::getPerms).collect(Collectors.toSet());
        String uuid = UUID.randomUUID().toString();
        LoginUser loginUser = new LoginUser();
        loginUser.setNickName(user.getNickName());
        loginUser.setUserName(user.getUserName());
        loginUser.setPermsList(permssionService.getPerms(user.getUserName()));
        loginUser.setRoleKeyList(permssionService.getRoleKeys(user.getUserName()));
        redisUtils.set(CACHE_PREFIX_TOKEN + uuid, new ObjectMapper().writeValueAsString(loginUser), 1800);
        //返回token
        Token token = new Token();
        token.setToken(uuid);
        return token;
    }

    @Override
    public void logout(String token) {
        redisUtils.del(CACHE_PREFIX_TOKEN + token);
    }
}
